<?php
include_once "../../../../common.php";
if(isset($_GET['id']) && is_numeric($_GET['id']) && !empty($_GET['id']))
{
	
	$query = "	SELECT 
					WC.* 
				FROM WebshopContactpersonen AS WC 
				WHERE WC.ID = '".str_replace("'","",$_GET['id'])."'";
	$result = $_MySQL->runQuery($query);
	$row = mysql_fetch_assoc($result);
	?>
	<table width="100%;">
		<tr>
			<td width="40%">
				Firstname
			</td>
			<td width="10px">:</td>
			<td>
				<input type="text" id="Firstname" value="<?=$row['Firstname'];?>" />
			</td>
		</tr>
		<tr>
			<td>
				Initials
			</td>
			<td>:</td>
			<td>
				<input type="text" id="Initials" value="<?=$row['Initials'];?>" />
			</td>
		</tr>
		<tr>
			<td>
				Surname
			</td>
			<td>:</td>
			<td>
				<input type="text" id="Surname" value="<?=$row['Surname'];?>" />
			</td>
		</tr>
		<tr>
			<td>
				Sex
			</td>
			<td>:</td>
			<td>
				<select id="SexValue">
					<option value="">UNKNOWN</option>
				<?php
				$sub_query = "SELECT * FROM Sexes ORDER BY Name";
				$sub_result = $_MySQL->runQuery($sub_query);
				while($sub_row = mysql_fetch_assoc($sub_result))
				{
					$selected = '';
					if($sub_row['ID'] == $row['Sex'])
						$selected = ' selected="selected"'
					?>
					<option value="<?=$sub_row['ID'];?>"<?=$selected;?>><?=$sub_row['Name'];?></option>
					<?php
				}
				?>
				</select>
			</td>
		</tr>
		<tr>
			<td>
				Insertion
			</td>
			<td>:</td>
			<td>
				<input type="text" id="Insertion" value="<?=$row['Insertion'];?>" />
			</td>
		</tr>
		<tr>
			<td>
				Function
			</td>
			<td>:</td>
			<td>
				<input type="text" id="Function" value="<?=$row['Function'];?>" />
			</td>
		</tr>
		<tr>
			<td>
				Email
			</td>
			<td>:</td>
			<td>
				<input type="text" id="Email" value="<?=$row['Email'];?>" />
			</td>
		</tr>
		<tr>
			<td>
				Mobile
			</td>
			<td>:</td>
			<td>
				<input type="text" id="Mobile" value="<?=$row['Mobile'];?>" />
			</td>
		</tr>
		<tr>
			<td>
				FirstNameBasis
			</td>
			<td>:</td>
			<td>
				<input type="checkbox" id="FirstNameBasis" value="1" <?=$row['FirstNameBasis']=='1'?'checked="checked"':'';?> />
			</td>
		</tr>
		<tr>
			<td colspan="3">
				<input type="button" value=" Opslaan " onClick="javascript: saveContactpersoon()" /> 
			</td>
		</tr>
	</table>
	<script>
		function saveContactpersoon()
		{
			var url = 'factory/saveContactpersoon.php';
			var FirstNameBasis = parseInt($('#FirstNameBasis:checked').val());
			var Mobile = $('#Mobile').val();
			var Firstname = $('#Firstname').val();
			var Initials = $('#Initials').val();
			var Surname = $('#Surname').val();
			var Insertion = $('#Insertion').val();
			var Function = $('#Function').val();
			var Email = $('#Email').val();
			var Sex = $('#SexValue').val();
			
			$.post(
					url,
					{
						ID:'<?=$_GET['id'];?>',
						FirstNameBasis:FirstNameBasis,
						Mobile:Mobile,
						Firstname:Firstname,
						Initials:Initials,
						Surname:Surname,
						Insertion:Insertion,
						Function:Function,
						Email:Email,
						Sex:Sex
					},
					function(data)
					{
						if(data == 'succes')
						{
							updateElement('KlantenContactpersonen');
					 	 	$(".popup").dialog('destroy');
						}
						else alert(data);
					}
			);
		}
	</script>
	<?php
}
